/*
 * @name: 官网banner
 * @Author: along
 * @Date: 2020-06-01
 */
<template>
    <div class="container zdWebsitBanner">
        <el-tag
            type="danger"
            style="margin-bottom: 10px;"
        >
            只展示列表中前六个banner
        </el-tag>
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">banner名称:</p>
                    <el-input
                        v-model="head.name"
                        placeholder="请输入内容"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">状态:</p>
                    <el-select
                        v-model="head.status"
                        placeholder="请选择"
                        size="small"
                        filterable
                        clearable
                        @change="fnChangeStatus()"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"/>
                    </el-select>
                </div>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="()=>{
                            this.$router.push({
                                name: 'zdWebsitBannerChange',
                                query: {
                                    banner_id: ''
                                }
                            })
                        }"
                    >
                        新建页面中的banner
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            ref="zdWebsitBanner"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="title"
                label="banner名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="pic_url"
                label="banner图片"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <!-- <p
                        @click="fnShowPreview(scope.row.pic_url)"
                        class="cursor-font"
                    >
                        查看图片
                    </p> -->
                    <img
                        :src="scope.row.pic_url"
                        alt="加载失败"
                        @click="fnShowPreview(scope.row.pic_url)"
                        style="width:106px;height:53px"
                        class="cursor-font"
                    >
                </template>
            </el-table-column>
            <el-table-column
                prop="sort"
                label="排序"
                align="center"
                sortable
            >
                <template slot-scope="scope">
                    <div class="sort-wrap">
                        {{ scope.row.sort }}
                        <span class="sort-spliter" />
                        <el-button
                            type="text"
                            class="btn-sort"
                            @click="fnEditSort(scope.row)"
                        >
                            <i class="el-icon-edit" />
                        </el-button>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    <span v-if="scope.row.status == 1">已显示</span>
                    <span v-if="scope.row.status == 0">已隐藏</span>
                </template>
            </el-table-column>
            <el-table-column
                prop="start_time"
                label="开始时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.start_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                prop="end_time"
                label="结束时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    {{ scope.row.end_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
            <el-table-column
                label="操作"
                align="center"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnEditBanner(scope.row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        v-if="scope.row.status == 0"
                        @click="fnBannerDisplay(scope.row)"
                    >
                        显示
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        v-if="scope.row.status == 1"
                        @click="fnBannerDisplay(scope.row)"
                    >
                        隐藏
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="fnDeleteBanner(scope.row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <!-- <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div> -->

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    /**
     * @param   {Object}     head                       筛选字段
     * @param   {Object}     page                       分页
     * @param   {Array}      tableData                  数据列表
     * @param   {OBject}     authResult                 权限列表
     */
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            head: {
                name: '',
                status: ''
            },
            statusList: [{
                value: 99,
                label: '全部'
            },{
                value: 1,
                label: '已显示'
            },{
                value: 0,
                label: '已隐藏'
            }],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableDataCopy: [],//拷贝数据，用于筛选
            tableData: [],
            authResult: {}
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{title: '官网'},{title: '官网自定义'},{title: '官网banner'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/wei4/base%5Cbanner%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword: this.head.name,
                scope: 4
                // status: this.head.status
            }, resp => {
                if (resp.code == 1) {
                    // this.tableData = resp.data.list;
                    this.sortTable(resp.data.list);
                    this.tableDataCopy = JSON.parse(JSON.stringify(resp.data.list));
                    // this.page.now_page = resp.data.page.now_page;
                    // this.page.total_count = resp.data.page.total_count;
                    // this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 排序
         */
        sortTable (arr) {
            let list = [];
            let now_time = new Date().getTime() / 1000;

            // 过期直接处理为已隐藏
            arr.forEach(option => {
                if((option.end_time < now_time && option.end_time !== 0) || option.start_time > now_time) {
                    option.status = 0;
                    list.push(option);
                } else {
                    list.push(option);
                }
            });

            // 升序排列
            let table = arr.sort((a,b) => {
                return a.sort - b.sort;
            });

            //升序显示排列
            let tableShow = table.sort((a,b)=>{
                return b.status - a.status;
            });

            this.tableData = tableShow;
        },

        /**
         * 选择状态
         */
        fnChangeStatus () {
            let list = [];

            if(this.head.status == 99 || this.head.status == '') {
                this.tableData = JSON.parse(JSON.stringify(this.tableDataCopy));
            }
            if(this.head.status == 1) {
                list = [];
                this.tableDataCopy.forEach(option=>{
                    if(option.status == 1) {
                        list.push(option);
                    }
                });

                this.tableData = list;
            }
            if (this.head.status == 0) {
                list = [];

                this.tableDataCopy.forEach(option=>{
                    if(option.status == 0) {
                        list.push(option);
                    }
                });

                this.tableData = list;
            }

            this.sortTable(this.tableData);
        },

        /**
         * 编辑banner
         */
        fnEditBanner (row) {
            this.$router.push({
                name: 'zdWebsitBannerChange',
                query: {
                    banner_id: row.id
                }
            });
        },

        /**
         * 显示or隐藏
         */
        fnBannerDisplay (row) {
            this.$post('/wei4/base%5Cbanner%5Cset_status',{
                id: row.id,
                status: row.status == 1 ? 0 : 1
            },resp=>{
                if (resp.code == 1) {
                    this.$set(row,'status',row.status == 1 ? 0 : 1);
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'warning',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 删除banner
         */
        fnDeleteBanner (row) {
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$post('/wei4/base%5Cbanner%5Cdelete',{
                    id: row.id
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '删除成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 排序
         */
        fnEditSort (row) {
            this.$prompt('请输入排序号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^([1-9]\d?|[1-9]\d+)$/,
                inputErrorMessage: '排序号格式不正确,请重新输入'
            }).then(({ value }) => {
                this.$post('/wei4/base%5Cbanner%5Cedit',{
                    id: row.id,
                    sort: value
                },resp=>{
                    if (resp.code == 1) {
                        this.getTableData();
                        this.$message({
                            type: 'success',
                            message: '操作成功'
                        });
                    } else if (resp.code == 0) {
                        this.$message({
                            type: 'warning',
                            message: resp.msg
                        });
                    }
                });
            });
        },

        /**
         * 查看图片
         */
        fnShowPreview(images) {
            this.$refs.preview.show([
                images
            ]);
        },

        /**
         * 数据排序
         */
        handleSortChanged(data) {
            console.log(data,'data');
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .zdWebsitBanner{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
    }
</style>
